﻿using System;
using System.Data;
using Complaint.Models.Dtos;
using NUnit.Framework;

namespace Complaint.Models.Test
{
    [TestFixture]
    public class PasswordQuestionModelTest
    {
        private string _title1, _title2;

        [TestFixtureSetUp]
        public void SetUp()
        {
            _title1 = Guid.NewGuid().ToString();
            _title2 = Guid.NewGuid().ToString();
        }

        [Test]
        public void ShouldCreateTestPasswordQuestion()
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new PasswordQuestionModel();
                var passwordQuestion = new PasswordQuestion { Question = _title1 };

                int countBefore = model.Retrieve().Count;
                model.Create(passwordQuestion);
                int countAfter = model.Retrieve().Count;

                Assert.That(countBefore == countAfter - 1);

                var c = model.FindById(passwordQuestion.PasswordQuestionID);
                Assert.That(c, Is.EqualTo(passwordQuestion));
            }
        }

        [Test]
        public void ShouldRetrieveByName()
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new PasswordQuestionModel();

                var c = model.FindByName(_title1);
                Assert.That(c.Count == 1);
            }
        }

        [Test]
        public void ShouldUpdate()
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new PasswordQuestionModel();
                var p = model.FindByName(_title1)[0];

                p.Question = _title2;
                model.Update(p);

                var passwordQuestion = model.FindById(p.PasswordQuestionID);
                Assert.That(passwordQuestion.Question, Is.Not.EqualTo(_title1));
                Assert.That(passwordQuestion.Question, Is.EqualTo(_title2));
            }
        }

        [Test]
        public void ShouldRetrieveByPasswordQuestion()
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new PasswordQuestionModel();
                var p1 = model.FindByName(_title1);
                var p2 = model.FindByName(_title2);

                Assert.That(p1.Count == 0);
                Assert.That(p2.Count == 1);
            }
        }

        [Test]
        public void ShouldDeleteTestPasswordQuestion()
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new PasswordQuestionModel();
                var passwordQuestion = model.FindByName(_title2)[0];

                model.Delete(passwordQuestion);

                var p = model.FindByName(_title2);

                Assert.That(p.Count == 0);
            }
        }

        [TestFixtureTearDown]
        public void CleanUp()
        {
            _title1 = null;
            _title2 = null;
        }
    }
}
